﻿
var obj = {
    productTypeId: '',
    productTypeNo: "",
    productTypeName: "",
    productSuperior: "",
    productRemarks: "",
    officeId: "",
    userid: "",
    createName: "",
    createTime: "",
    deleteName: "",
    deleteTime: "",
}

$(function () {
    Shows()
});

var productTypeNo = $("#productTypeNos").val();
var productTypeName = $("#productTypeNames").val();
var index = 1;
var size = 100;
var tacount = 0;



function Shows() {

    layui.use('table', function () {
        var table = layui.table;

        $.ajax({
            url: '../Product/GEtProductTypeShow',
            type: 'get',
            data: {
                index: index,
                size: size,
                productTypeNo: $("#productTypeNos").val(),
                productTypeName: $("#productTypeNames").val(),
                token: localStorage.getItem("token")
            },
            success: function (data) {
                res = JSON.parse(data);
                console.log(res);
                if (res.code == 0) {
                    tacount = res.data.tacount
                    var inst = table.render({
                        elem: productTypeShow,
                        data: res.data.list,
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports'],
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'productTypeNo', title: '类别编号' },
                            { field: 'productTypeName', title: '类别名称' },
                            {
                                field: 'productTypes',
                                title: '上级类别',
                                templet: function (d) {
                                    return d.productTypes ? d.productTypes.productTypeName : '-';
                                }
                            },
                            {
                                field: 'createDate',
                                title: '创建时间',
                                templet: function (d) {
                                    if (d.createDate) {
                                        return d.createDate.split('T')[0];
                                    }
                                    return '2024-11-24';
                                }
                            },
                            { field: 'productRemarks', title: '备注' },
                            {
                                field: '', title: '操作', templet: function (d) {
                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editProductType(' + d.productTypeId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="productTypeDel(' + d.productTypeId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                                }
                            }
                        ]],
                        page: true, // 是否显示分页
                        count: tacount,
                        limits: [6, 10, 18],
                        limit: 5,// 每页默认显示的数量
                        curr: index,
                    });
                } else {
                    console.log(res.msg)
                }
            },
        })
    })

}




//刷新重置
function refesh() {
    location.reload();
}
//添加商品类别
layui.use(function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;


    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                area: '500px',
                resize: false,
                shadeClose: true,
                title: '新增产品类别',
                content: `<form class="layui-form layui-form-pane" action="" style="text-align: center;width:100%">
                    <div class="layui-form-item">
                        <label class="layui-form-label">类别编号：</label>
                        <div class="layui-input-inline">
                        <input type="text" id="productTypeNo" name="productTypeNo" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">类别名称：</label>
                        <div class="layui-input-inline">
                        <input type="text" id="productTypeName" name="productTypeName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div style="width:350px" class="layui-form layui-row layui-col-space16">
                        <label class="layui-form-label">上级类别：</label>
                        <div class="layui-input-block">
                            <select id="productSuperior" name="productSuperior" lay-filter="mySelectFilter">
                            <option value="0">请选择</option>
                                                              <option value="1">智能设备</option>
                                                              <option value="2" checked>高值耗材</option>
                                                              <option value="3">低值易耗品</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注：</label>
                        <div class="layui-input-inline">
                        <input type="text" id="productRemarks" name="productRemarks" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn" lay-submit lay-filter="demo-login">提交<button>
                        <button type="reset" class="layui-btn layui-btn-primary">取消<button>
                    </div>
                </form>`,
                success: function () {
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field; // 获取表单字段值
                        // 显示填写结果，仅作演示用
                        // 此处可执行 Ajax 等操作
                        // …
                        $.ajax({
                            url: '../Product/GEtProductTypeAdd',
                            type: 'post',
                            //contentType: 'application/json',
                            data: {
                                data: JSON.stringify(field)
                            },
                            success: function (res) {
                                console.log(field)
                                console.log(res)
                                if (res.code != "") {
                                    layer.alert("添加成功");
                                    location.href = "/Product/ShowIndex"
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });
        }
    })
    //反填
    window.editProductType = function (productTypeId) {
        $.ajax({
            url: '../Product/GEtProductTypeFind',
            type: 'get',
            data: {
                productTypeId: productTypeId,
                token: localStorage.getItem("token")
            },
            success: function (res) {

                //var jsonString = JSON.stringify(res)
                var parsedData = JSON.parse(res);
                console.log(parsedData)
                $(parsedData.data).each(function () {
                    console.log(parsedData.data)
                    $("#productTypeId").val(this.productTypeId)
                    $("#productTypeNo").val(this.productTypeNo)
                    $("#productTypeName").val(this.productTypeName)
                    $("#productSuperior").val(this.productSuperior)
                    $("#productRemarks").val(this.productRemarks)
                    $("#officeId").val(this.officeId)
                    $("#userid").val(this.userid)
                    $("#createName").val(this.createName)
                    $("#createTime").val(this.createTime)
                    $("#deleteName").val(this.deleteName)
                    $("#deleteTime").val(this.deleteTime)


                    obj.productTypeId = this.productTypeId
                    obj.productTypeNo = this.productTypeNo
                    obj.productTypeName = this.productTypeName
                    obj.productSuperior = this.productSuperior
                    obj.productRemarks = this.productRemarks
                    obj.officeId = this.officeId
                    obj.userid = this.userid
                    obj.createName = this.createName
                    obj.createTime = this.createTime
                    obj.deleteName = this.deleteName
                    obj.deleteTime = this.deleteTime

                })
            }
        });
        layer.open({
            type: 1,
            title: '修改产品类别',
            content: `<form class="layui-form layui-form-pane" action="" style="text-align: center;width:100%">
                        <div class="layui-form-item">
                            <label class="layui-form-label">类别编号：</label>
                            <div class="layui-input-inline">
                            <input type="text" id="productTypeNo" name="productTypeNo" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">类别名称：</label>
                            <div class="layui-input-inline">
                            <input type="text" id="productTypeName" name="productTypeName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div style="width:350px" class="layui-form layui-row layui-col-space16">
                            <label class="layui-form-label">上级类别：</label>
                            <div class="layui-input-block">
                            <select id="mySelect" name="productSuperior" lay-filter="mySelectFilter">
                                                                  <option value="1">智能设备</option>
                                                                  <option value="2">高值耗材</option>
                                                                  <option value="3">低值易耗品</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注：</label>
                            <div class="layui-input-inline">
                            <input type="text" id="productRemarks" name="productRemarks" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" lay-submit lay-filter="demo-login">提交<button>
                            <button type="reset" class="layui-btn layui-btn-primary">取消<button>
                        </div>
                    </form>`,
            area: '800px',
            success: function () {
                var token = localStorage.getItem("token")
                var obj1 = {
                    productTypeId: '',
                    productTypeNo: "",
                    productTypeName: "",
                    productSuperior: "",
                    productRemarks: "",
                    officeId: "",
                    userid: "",
                    createName: "",
                    createTime: "",
                    deleteName: "",
                    deleteTime: "",
                }

                // 对弹层中的表单进行初始化渲染
                form.render();
                // 表单提交事件
                form.on('submit(demo-login)', function (data) {
                    var field = data.field; // 获取表单字段值
                    // 显示填写结果，仅作演示用
                    // 此处可执行 Ajax 等操作
                    obj1.productTypeId = obj.productTypeId
                    obj1.productTypeNo = field.productTypeNo
                    obj1.productTypeName = field.productTypeName
                    obj1.productSuperior = field.productSuperior
                    obj1.productRemarks = field.productRemarks
                    obj1.officeId = obj.officeId
                    obj1.userid = obj.userid
                    obj1.createName = obj.createName
                    obj1.createTime = obj.createTime
                    obj1.deleteName = obj.deleteName
                    obj1.deleteTime = obj.deleteTime

                    $.ajax({
                        url: '../Product/ProductTypeUpdate',
                        type: 'post',
                        //contentType: 'application/json',
                        data: { obj1, token: token },
                        success: function (res) {
                            console.log(res)
                            var list = JSON.parse(res)
                            if (list.code != "") {
                                layer.alert("修改成功");
                                //  location.href = "/Product/ProductTypeView"
                            }
                            else {
                                layer.alert("修改失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        });

    }

});



function Update(productId) {

    localStorage["productId"] = productId;//把要编辑的商品id存到 localStorage["id"]
    console.log(localStorage["productId"])
}




//逻辑删除
window.productTypeDel = function (productTypeId) {
    var obj = JSON.stringify({
        "productTypeId": productTypeId
    })

    layer.confirm('确定要删除该条数据吗？', function (index) {

        $.ajax({
            type: 'post',
            url: '../Product/ProductTypeDel',
            //contentType: 'application/json',//浏览器里报415错
            data: { data: obj, token: localStorage.getItem("token") },
            success: function (res) {
                if (res != "") {
                    layer.msg('删除成功', { icon: 1 });
                    Shows()
                    return;
                } else {
                    layer.alert("删除失败!");
                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }

        })
        layer.close(index);
    })

}



//产品批量删除
$('#batchDeleteButton').click(function () {
    var table = layui.table;
    // 获取选中行的数据（这里主要是获取warehouseId）
    var checkStatus = table.checkStatus('productTypeShow');
    var selectedData = checkStatus.data.map(function (item) {
        return item.productTypeId;
    });

    if (selectedData.length === 0) {
        layer.msg("请至少选择一条要删除的数据", { icon: 2 });
        return;
    }

    console.log(selectedData);
    layer.confirm('确定要批量删除选中的' + selectedData.length + '条数据吗？', function (index) {
        // var productIds = {
        //     ProductTypeId: selectedData
        // }
        // 发送批量删除请求到后端
        $.ajax({
            url: "../Product/ProductTypeDelRange", // 后端处理批量删除的接口地址，需根据实际情况调整
            type: "post",
            // contentType: "application/json",
            data: {
                productTypeId: selectedData,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res);
                if (res.data > 0) {
                    layer.msg("批量删除成功", { icon: 1 })
                    // 重新加载表格数据，展示更新后的列表
                    Shows()
                } else {
                    layer.msg("批量删除失败:" + res.message, { icon: 2 });
                }
            },
            error: function () {
                console.log("批量删除数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });
    })
})